//
//  CYCreditAddWebVC.m
//  CreditAddressBook
//
//  Created by mosu on 2016/11/4.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYCreditAddWebVC.h"
#import "CYJumpsTool.h"
#import "CYLoginHttpTool.h"
#import "CYDiscoveryVC.h"
#import "MBProgressHUD+MJ.h"
#import "CYRegeditVController.h"
#import "CYLoginVController.h"
#import "LEUUID.h"
#import "CYCreditNewLitmitVC.h"
#import "CYAddCreditVC.h"
#import "UIAlertView+Block.h"

@interface CYCreditAddWebVC ()<UIWebViewDelegate>
@property (nonatomic, strong)UIWebView *webview;
@property (nonatomic, strong)UIView *hiview;
@property (nonatomic, assign)BOOL isfirstLoad;
@property (nonatomic, strong)NSString *url1;

@end

@implementation CYCreditAddWebVC

- (void)viewDidLoad {
    [super viewDidLoad];
     //增值服务
     self.navigationController.interactivePopGestureRecognizer.enabled = NO;
     //[self.navigationBar setBackgroundColor:[UIColor blackColor]];
     CYUser *user = [CYLoginHttpTool userData].user;
     self.view.backgroundColor = [UIColor blackColor];
     _webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen ]bounds].size.width,[[UIScreen mainScreen ]bounds].size.height)];
     _webview.backgroundColor = CYColor(68, 68, 68, 1);
     _webview.delegate = self;
     //http://192.168.0.199:8080/creditloan/center.htm  http://www.credit-manage.com:8058/creditloan/center.htm
     //http://192.168.0.254:8058/creditloan/center.htm  http://192.168.0.172:8058/creditloan/center.htm 用户系统测试ZTT
     
     //测试：信用收益  http://192.168.0.199:8080/creditloan/creditDetail.htm
     //测试：增值服务   http://192.168.0.199:8080/creditloan/getzzfwApp.htm
     
     NSString *version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey];      //获取项目版本号
     //测试:http://192.168.0.115:8080/creditloan/getInComes.htm
     
     if (user) {
          NSString *url= [NSString stringWithFormat:@"http://www.credit-manage.com:8058/creditloan/getzzfwApp.htm?&userId=%@&token=%@&versionNo=%@&type=",user.userId,user.token,version];
          NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:url]];
          [_webview loadRequest:request];
     }else{
          
          [CYJumpsTool jumpToLogin:self];
          
     }
     
     //添加滑动返回
     UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipes:)];
     rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
     [_webview addGestureRecognizer:rightSwipeGestureRecognizer];
     // Do any additional setup after loading the view.
     [self.view addSubview:_webview];
     
     self.isfirstLoad = YES;

    // Do any additional setup after loading the view.
}

/**
 *  添加滑动返回
 */
- (void)handleSwipes:(UISwipeGestureRecognizer *)sender {
     if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
          [[self navigationController] setNavigationBarHidden:NO animated:NO];
          [self.navigationController popViewControllerAnimated:YES];
     }
}

#pragma mark -
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
//     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"网络连接失败" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:nil, nil];
//     [alert showAlertViewWithCompleteBlock:^(NSInteger buttonIndex) {
//          if (buttonIndex == 0) {
//               [[self navigationController] setNavigationBarHidden:NO animated:NO];
//               [self.navigationController popViewControllerAnimated:YES];
//               
//          }
//     }];
     
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
     UIButton *Shopligonbtn = [UIButton buttonWithType:UIButtonTypeCustom];
     Shopligonbtn.frame = CGRectMake(self.view.x+10,self.view.height-50,80,30);
     Shopligonbtn.backgroundColor = CYColor(0, 170, 238, 1);
     Shopligonbtn.layer.cornerRadius = 5.0f;
     [Shopligonbtn setTitle:@"返回发现" forState:UIControlStateNormal];
     Shopligonbtn.titleLabel.font = [UIFont systemFontOfSize:14];
     Shopligonbtn.titleLabel.textColor = [UIColor whiteColor];
     Shopligonbtn.showsTouchWhenHighlighted = YES;
     //[self.view addSubview:Shopligonbtn];
     [Shopligonbtn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpOutside];
     //如果加载完成webview后，navi隐藏,加载失败，提供系统导航栏给用户返回
     //[self.navigationController setNavigationBarHidden:YES animated:NO];
     [MBProgressHUD hideHUDForView:self.view];
     
}

-(void)click{
     NSLog(@"被点击");
     [self.navigationController popViewControllerAnimated:YES];
     
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
     //[self.view addSubview:_hiview];
     //[MBProgressHUD hideHUDForView:self.view];
     NSString *url = [[request URL] absoluteString];
     
     if([url rangeOfString:@"report"].location !=NSNotFound)//_roaldSearchText
     {
          NSString *str = url;
          CYLog(@"str = %@",str);
          NSString *str1 = [str substringFromIndex:12];//截取掉下标11之后的字符串
          CYLog(@"str1 = %@",str1);
          if (str1 && !str1.isEmpty) {
               
               UIPasteboard *pab = [UIPasteboard generalPasteboard];
               [pab setString:str1];
               if (pab == nil) {
                    [MBProgressHUD showError:@"复制失败"];
                    
               }else
               {
                    [MBProgressHUD showSuccess:@"已复制"];
                    
               }
               
               
          }
     }
     else
     {
          NSLog(@"no");
     }
     NSLog(@"url = %@",url);
     //h5内返回控件字段方法:loanlist
     if([url hasSuffix:@"loanlist"]) {
          dispatch_async(dispatch_get_main_queue(), ^{
               //返回上一级时，取消nav的隐藏
               [[self navigationController] setNavigationBarHidden:NO animated:NO];
               //js交互oc获取到h5内的返回按钮后，点击返回，popvc
               [self.navigationController popViewControllerAnimated:YES];
          });
          return NO;
     }
     
     //h5内提升额度控件方法:task
     if ([url hasSuffix:@"task"]) {
          //do anything you want
          CYUser *user = [CYLoginHttpTool userData].user;
          if (user.certificateExamineStatus.intValue == 2) {
               CYCreditNewLitmitVC *newlimit = [[CYCreditNewLitmitVC alloc]init];
               [self.navigationController pushViewController:newlimit animated:YES];
               
          }else{
               CYAlertView(@"您需要进行实名绑卡后才可以使用身价功能");
               
          }
          
          return NO;
     }
     
     if ([url hasSuffix:@"reg"]) {
          //do something
          CYLog(@"beclick");
          CYAlertView(@"请前往“我”－“注册”进行账号注册");
          return NO;
          //        CYRegeditVController *regt = [CYRegeditVController le_loadFromStoryBoard:[CYJumpsTool loginSB]];
          //        regt.hidesBottomBarWhenPushed = YES;
          //        [self.navigationController pushViewController:regt animated:YES];
     }
     
     return YES;
}

- (void)viewDidAppear:(BOOL)animated {
     [super viewDidAppear:animated];
     //    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
     [[self navigationController] setNavigationBarHidden:YES animated:NO];
}

- (void)viewWillDisappear:(BOOL)animated {
     [super viewWillDisappear:animated];
     //    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
     [[self navigationController] setNavigationBarHidden:NO animated:NO];
}
- (void)dealloc {
     
     [CYNotificationCenter removeObserver:self];
     [_webview didMoveToSuperview];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
